<?php

require_once 'SecurityController.php';

class PorcinosController extends SecurityController
{

    public function init()
    {
        parent::init();
//        $this->isSomeOne(true);
    }

    public function indexAction()
    {
        parent::indexAction(new Application_Model_PorcinoMapper());
    }

    public function formAction()
    {
    }

    public function removeAction()
    {
        parent::removeAction(new Application_Model_PorcinoMapper());
    }

    public function hembrasAction()
    {
        $request = $this->getRequest();
        $format = $request->getParam('format');
        $mapper = new Application_Model_PorcinoMapper();
        if ($format == 'json') {
            $entries = $mapper->fetchHembrasByGranja('1');
            $data = array();
            if (!is_array($entries)) {
                array_push($data, $entries->toArray());
            } else {
                foreach ($entries as $entry) {
                    array_push($data, $entry->toArray());
                }
            }

            echo Zend_Json_Encoder::encode(array('rows'=>$data));
            exit();
        } else {

            $id_granja = $this->getGranja()->getId();

            $this->view->entries = $mapper->fetchHembrasByGranja($id_granja);

            $mapper = new Application_Model_TipoEventoMapper();
            //TODO filter olny the events to 'hembras'
            //TODO renmae eventos for tipos_eventos
            $this->view->eventos = $mapper->fetchAll();
        
    }}

    public function formHembrasAction()
    {

        $extendedForm = new Application_Form_Porcino();
        $extendedForm->getElement("sexo")->setValue('h');

        $id_porcino = parent::formAction($extendedForm, new Application_Model_Porcino(), new Application_Model_PorcinoMapper(), false);
        if ($this->getRequest()->isPost() && $id_porcino != null) {
            $model = new Application_Model_PorcinoGranja();
            $mapper = new Application_Model_PorcinoGranjaMapper();

            $model->setGranjaId($this->getGranja()->getId());
            $model->setPorcinoId($id_porcino);
            $mapper->save($model);
            $this->_redirect("/porcinos/hembras");
        
    }}

    public function verracosAction()
    {
        $request = $this->getRequest();
        $format = $request->getParam('format');
        $mapper = new Application_Model_PorcinoMapper();
        if ($format == 'json') {
            $entries = $mapper->fetchVerradosByGranja('1');
            $data = array();
            if (!is_array($entries)) {
                array_push($data, $entries->toArray());
            } else {
                foreach ($entries as $entry) {
                    array_push($data, $entry->toArray());
                }
            }

            echo Zend_Json_Encoder::encode(array('rows'=>$data));
            exit();
        
    }}

    public function eventosAction()
    {
    }

    public function saveAction()
    {
        $request = $this->getRequest();
        $datos = array(
            'codigo' => $request->getParam('codigo'),
            'ubicacion_pocilga' => $request->getParam('ubicacion_pocilga'),
            'ubicacion_cuarto' => $request->getParam('ubicacion_cuarto'),
            'origen' => $request->getParam('origen'),
            'fecha_nacimiento' => $request->getParam('fecha_nacimiento'),
            'fecha_llegada' => $request->getParam('fecha_llegada'),
            'nombre' => $request->getParam('nombre'),
            'camada' => $request->getParam('camada'),
            'fecha_baja' => $request->getParam('fecha_baja'),
            'activo' => $request->getParam('activo'),
            'madre_id' => $request->getParam('madre_id')

        );

        $porcino = new Application_Model_Porcino($datos);
        $mapper = new Application_Model_PorcinoMapper();
        $mapper->save($porcino);
    }


}





